# load libraries ----
# reporting
library(knitr)
# visualization
library(ggplot2)
library(ggthemes)
library(patchwork)
# data wrangling
library(dplyr)
library(tidyr)
library(readr)
library(skimr)
library(janitor)
library(magrittr)
library(lubridate)
# General Lake Model
library(GLMr)
library(glmtools)
# set other options ----
options(scipen=999)
knitr::opts_chunk$set(
tidy = FALSE,
message = FALSE,
warning = FALSE)30 Lake thermal structures II
Learning Objectives
After completing this tutorial you should be able to
- formulate the importance of simulated data to exploring complex questions
- set up and run the General Lake Model (GLM) to simulate lake thermal structure
- design climate scenarios, simulate their effect on lake thermal structure, and interpret the model output
Download the directory for this project here, make sure the directory is unzipped and move it to your bi328 directory. You can open the Rproj for this module either by double clicking on it which will launch Rstudio or by opening Rstudio and then using File > Open Project or by clicking on the Rproject icon in the top right of your program window and selecting Open Project.
There should be a file named 30_thermal-structure-II.qmd in that project directory. Use that file to work through this tutorial - you will hand in your rendered (“knitted”) quarto file as your homework assignment. So, first thing in the YAML header, change the author to your name. You will use this quarto document to record your answers. Remember to use comments to annotate your code; at minimum you should have one comment per code set1 you may of course add as many comments as you need to be able to recall what you did. Similarly, take notes in the document as we discuss discussion/reflection questions but make sure that you go back and clean them up for “public consumption”.
1 You should do this whether you are adding code yourself or using code from our manual, even if it isn’t commented in the manual… especially when the code is already included for you, add comments to describe how the function works/what it does as we introduce it during the participatory coding session so you can refer back to it.
Before you get started, let’s make sure to read in all the packages we will need.
30.1 Develop your own climate scenario
Now that we have an idea of how to interact with the model, and we’ve tested the quality of our model by comparing observed and simulated data sets let’s use the model to simulate a data set for a specific climate model.
You are going to develop two climate scenarios involving an extreme event (e.g. hurricane) and a more gradual change (e.g. increase in observed air temperature).
For some inspiration on your climate scenario you can check out a visualization of high and low emission scenarios and how they impact temperature and precipitation. You can also take a look at global projections to see how much change is expected for various models and metrics.
Next, you will need to modify the input data (met_hourly.csv) to reflect the climate scenario you want to run.
Make sure to follow the instructions on how to do this closely to make sure that you will be able to use your input file without issues to run the model.
In your project folder create a new folder called sim1. Then make a copy of met_hourly.csv and place it into that folder and rename it sim1_met_hourly.csv. Be sure to do this before you open it and make any changes to the file. Also add a copy of glm2.nml to this folder.
Before you start - any time you open a *_met_hourly.csv file in Microsoft Excel it will alter the date/time formatting of the file so that GLM cannot read it. To avoid this error you will need to follow the steps below any time you change your met file.
- Just to be safe, copy and paste an extra version of the
met_hourly.csvfile in yoursimfolder so that you have a backup instead you make mistakes and want to go back to it. rename itmet_hourly_original.csvor similar and be sure not to open this file in excel. - Open the
sim1/sim1_met_hourly.csvfile in Excel. Manipulate the input variables to reflect the weather event/climate scenario you have designed2. The order of columns does not matter but you can only have one of for each variable and the columns names should not be changed. - Once you have finished editing your file, reformat your date/time column.
- highlight the
timecolumn in Excel - click on
Format Cellsand then onCustom - in the
TypeorFormattingbox, change the default toYYYY-MM-DD hh:mm:ssexactly - this is the only format that GLM recognizes. - save the file once you are done and close the
csvfile - any time you open the file in Excel you will need to format this column and save the change!
- highlight the
- Read in the altered
met_hourlyfile to Rstudio, convert the format of the date/time column and write it back out to file.
2 A note on units: rain should be in meters per day, usually we measure precipitation in mm/d but make sure to convert to m/d by multiplying by 0.001 when you enter it into your met file
# make sure simulation 1 is correctly formatted ----
# read in altered met file & convert time format for GLM
metadata <- read_delim("sim1/sim1_met_hourly.csv", delim = ",") %>%
mutate(time = as.POSIXct(strptime(time, "%Y-%m-%d %H:%M:%S", tz="EST")))
# write back to file
write_delim(metadata, "sim1/sim1_met_hourly.csv", delim = ",")
# make sure simlation 2 is correctly formatted ----
# read in altered met file & convert time format for GLM
metadata <- read_delim("sim2/sim2_met_hourly.csv", delim = ",") %>%
mutate(time = as.POSIXct(strptime(time, "%Y-%m-%d %H:%M:%S", tz="EST")))
# write back to file
write_delim(metadata, "sim2/sim2_met_hourly.csv", delim = ",")Any time you alter the met file you will need to go back through these steps!
Before we can run the simulations, we will also need to edit the glm2.nml file to change the name of your input meteorological file so that it knows what file it should be looking for to get the meteorological data to run the model with.
Open the nml file that you have placed in your sim1 folder in a text editor3 and scroll down to the meteorology section. Here, change the meteo_fl entry to your new file name (sim1_met_hourly.csv) for your climate scenario4.
3 You can also open directly in Rstudios built in text editor if you prefer.
4 Pro tip: Us the Find option to locate the nml file. Details count: Make sure that your quotes ' around the file name are upright and not slanted!
Once you’ve edited the met file name in your nml file, double check that you’ve made sure that it’s correct. You can do this by reading in the file.
# change the path to new sim folder
nml_file <- "sim1/glm2.nml"
# read altered nml file
nml <- read_nml(nml_file)
# check met file name
get_nml_value(nml, "meteo_fl")If you made the changes correctly the output should list the name of the meteorological file you specified that describes your weather/climate scenario (sim1_met_hourly.csv).
Once you have completed this you are ready to run your climate scenarios.
Repeat the process to create your second simulation.
30.2 Run climate scenario 1
Now, let’s run the scenario.
# run model for climate scenario
run_glm("sim1/", verbose = TRUE)Our next step will be specifying the file path for our output so we can use the functions from glmtools to process the simulation output. Since this is our first climate scenario, we’ll name the variable climate1. Contrast this with our variable baseline that has the simulated output for current, typical meteorological conditions across a year based on the met_hourly.csv input file. Keep in mind that we use the same parameters to describe the link for both simulations - so we are keeping the lake the same and comparing different scenarios for the drivers of thermal structure5.
5 The other type of comparison you could make is having the same conditions for drivers and two or more different lakes.
# define file path for output for baseline scenario
baseline <- file.path("sim", 'output.nc')
# define file path for output for climate scenario 1
climate1 <- file.path("sim1", "output.nc")Next, let’s pull the surface temperature for our first climate scenario6.
6 Recall, that we did this earlier or our baseline scenario and stored that information in the data.frame surf_temp.
surf_temp_cl1 <- get_var(file = climate1, "temp", reference = "surface", z_out = c(1)) %>%
rename(Scenario1_Temp = temp_1)30.3 Run climate scenario 2
Now, let’s run the scenario. Make sure that your folder sim2 has the appropriate met data and that you have changed the filename in the nml file accordingly.
# run model for climate scenario
run_glm("sim2/", verbose = TRUE)Again, we will want to specify the file path for our output - we’ll name this variable climate2. So now we have three file path variables baseline, climate1, and climate2. We will use those in the next section to plot the results of our simulations.
# define file path for output
climate2 <- file.path("sim2", "output.nc")Next, let’s pull the surface temperature for our second scenario climate scenario.
surf_temp_cl2 <- get_var(file = climate1, "temp", reference = "surface", z_out = c(1)) %>%
rename(Scenario2_Temp = temp_1)30.4 Compare your results
We have three scenarios to compare, your baseline which is the scenario based on current conditions, a simulation of a short-term climate/weather event (climate1), and a gradual change (climate2)7.
7 Depending on how you set this up the names might be swapped for your scenarios
We currently have our surface temperature data in three separate data sets. We want them to be in a single data set. The first step will be to combine all three data sets. We have a column in common (DateTime), so we will want to use left_join().
Dates are notoriously messy to work with, so we will use mutate to make sure that all of them are in the same format first, using mutate().
surf_temp <- surf_temp %>%
mutate(DateTime = as.POSIXct(strptime(DateTime, "%Y-%m-%d", tz="EST")))
surf_temp_cl1 <- surf_temp_cl1 %>%
mutate(DateTime = as.POSIXct(strptime(DateTime, "%Y-%m-%d", tz="EST")))
surf_temp_cl2 <- surf_temp_cl2 %>%
mutate(DateTime = as.POSIXct(strptime(DateTime, "%Y-%m-%d", tz="EST")))
surf_temp_all <- surf_temp %>%
left_join(surf_temp_cl1) %>%
left_join(surf_temp_cl2)Before you can plot the data, you will need to create a tidy data set. Currently our column names for each of the scenarios are actually values, so it isn’t a tidy data set.
tidy_temp <- surf_temp_all %>%
pivot_longer(names_to = "scenarios", values_to = "temperature", 2:4)Now it will be easy to create your summary plot!
30.5 Acknowledgments
These activities are based on the EDDIE Climate Change Effects on Lake Temperatures module.8
8 Carey, C.C., S. Aditya, K. Subratie, V. Daneshmand, R. Figueiredo, and K.J. Farrell. 24 August 2020. Macrosystems EDDIE: Climate Change Effects on Lake Temperatures. Macrosystems EDDIE Module 1, Version 2.